package com.waimai.controllor;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.waimai.adapter.RestaurantAdapter;
import com.waimai.dao.DBDao;
import com.waimai.entity.Address;

public class DBControllor extends BaseControllor {
	// 使用中的表
	// private static String RESTAURANT_TABLE = DaoConstant.RESTAURANT_TABLE;
	// private static String RA_RELATIONSHIP_TABLE =
	// DaoConstant.RA_RELATIONSHIP_TABLE;
	// 没使用的表
	// private static String UNUSE_RESTAURANT_TABLE =
	// DaoConstant.RESTAURANT_TABLE_2;
	// private static String UNUSE_RA_RELATIONSHIP_TABLE =
	// DaoConstant.RA_RELATIONSHIP_TABLE_2;
	// 显示哪一套
	// private static int TABLE_STATUS = DaoConstant.TABLE_STATUS_1;

	private static DBControllor controllor = null;

	private static DBDao dao = new DBDao();

	public static DBControllor getInstance() {
		if (controllor == null) {
			controllor = new DBControllor();
		}
		return controllor;
	}

	/**
	 * 
	 * 
	 * @param restaurants
	 */
	public void updateRestaurants(Map<String, RestaurantAdapter> restaurantMap) {
		before();
		Set<Entry<String, RestaurantAdapter>> set = restaurantMap.entrySet();
		Iterator<Entry<String, RestaurantAdapter>> it = set.iterator();
		while (it.hasNext()) {
			RestaurantAdapter adapter = ((Entry<String, RestaurantAdapter>) it
					.next()).getValue();
			if (dao.updateRestaurant(adapter) == 0) {
				dao.addRestaurant(adapter);
			}
			if (dao.updateRA_Relationship(adapter) == 0) {
				dao.addRA_Relationship(adapter);
			}
		}
		after("addRestaurantsAndRelationshipToDB ");
	}

	public Address[] getAddresses() {
		List<Address> l = dao.selectAddress();
		Address[] addresses = new Address[l.size()];
		l.toArray(addresses);
		return addresses;
	}

	// /**
	// *
	// *
	// * @param restaurants
	// */
	// public void addRestaurants(List<RestaurantAdapter> restaurants) {
	// before();
	// for (RestaurantAdapter adapter : restaurants) {
	// dao.addRestaurant(adapter, UNUSE_RESTAURANT_TABLE);
	// dao.addRA_Relationship(adapter, UNUSE_RA_RELATIONSHIP_TABLE);
	// }
	// after("addRestaurantsAndRelationshipToDB ");
	// }
	//
	// public List<Address> getAddAddress() {
	// return dao.selectAddress();
	// }
	//
	// /**
	// * 更换表 更换表1，表1应该空，表2做查询 只对餐厅和关系表做更换
	// *
	// */
	// public void changeTablesStatus() {
	// TABLE_STATUS = ~TABLE_STATUS;
	// switch (TABLE_STATUS) {
	// case DaoConstant.TABLE_STATUS_1:
	// // RESTAURANT_TABLE = DaoConstant.RESTAURANT_TABLE_1;
	// // RA_RELATIONSHIP_TABLE = DaoConstant.RA_RELATIONSHIP_TABLE_1;
	// UNUSE_RESTAURANT_TABLE = DaoConstant.RESTAURANT_TABLE_2;
	// UNUSE_RA_RELATIONSHIP_TABLE = DaoConstant.RA_RELATIONSHIP_TABLE_2;
	// break;
	// case DaoConstant.TABLE_STATUS_2:
	// // RESTAURANT_TABLE = DaoConstant.RESTAURANT_TABLE_2;
	// // RA_RELATIONSHIP_TABLE = DaoConstant.RA_RELATIONSHIP_TABLE_2;
	// UNUSE_RESTAURANT_TABLE = DaoConstant.RESTAURANT_TABLE_1;
	// UNUSE_RA_RELATIONSHIP_TABLE = DaoConstant.RA_RELATIONSHIP_TABLE_1;
	// break;
	// }
	// }
	//
	// /**
	// * 只对餐厅和关系表做更换 清空没使用的表
	// *
	// */
	// public void clearUnuseTables() {
	// if (!(dao.clearTable(UNUSE_RESTAURANT_TABLE) && dao
	// .clearTable(UNUSE_RA_RELATIONSHIP_TABLE))) {
	// Log.e("clearUnuseTables : error");
	// }
	// }
	//
	// public int getTableStatus() {
	// return TABLE_STATUS;
	// }
}
